package com.tywj.panda.home.notice

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.collection.ArrayMap
import com.alibaba.android.arouter.facade.annotation.Route

import com.chad.library.adapter.base.BaseQuickAdapter
import com.tywj.panda.home.R
import com.tywj.panda.home.entity.NoticeEntity
import com.tywj.panda.home.route.HomeRoute
import com.tywj.lib.core.common.util.BarUtil
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.net.retrofit.request.BaseRequest
import com.tywj.panda.customer.common.view.paging.BasePagingActivitySearch
import com.tywj.panda.home.api.HomeApi

/**
 * @desc 消息通知界面
 * @author June Yang
 * @date 2020/5/27
 */
@Route(path = HomeRoute.NOTICE_LIST)
class NoticeActivity : BasePagingActivitySearch<NoticeEntity, NoticeEntity.DataBean>(
    NoticeEntity.DataBean::class.java
) {

    companion object {
        const val REQUEST_NOTICE_DETAIL = 1001
    }

    // 进入通知详情页的消息id
    private var detailId = ""


    override fun getRecyclerViewAdapter(): BaseQuickAdapter<NoticeEntity.DataBean, *> = NoticeAdapter()

    override fun getRequest(): BaseRequest<*> = HomeApi.listNotice()

    override fun getRequestParams(): ArrayMap<String, String> = HomeApi.listNoticeParams()

    override fun getRequestParamsLoadMore(): ArrayMap<String, String> {
        return HomeApi.listNoticeParams(mAdapter.data.last().createDate)
    }


    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        BarUtil.setStatusBarColorRes(this, R.color.common_white)
        mTitleLayout.setTitleText(getString(R.string.home_system_notice))

        mTitleLayout.setLeftImageResource(R.drawable.common_back_black)
        mTitleLayout.setLayoutBackgroundColor(ResourcesUtil.getColor(R.color.common_white))
        mTitleLayout.setTitleColor(ResourcesUtil.getColor(R.color.common_black_33))

        addItemDecoration(R.dimen.dp_15)
    }

    override fun initListener() {
        super.initListener()

        mAdapter.setOnItemClickListener { _, _, position ->
            val notice = mAdapter.data[position]
            detailId = notice.id

            if (position == 0) {
                HomeRoute.toNoticeDetail(notice, true).navigation(this, REQUEST_NOTICE_DETAIL)
            } else {
                HomeRoute.toNoticeDetail(notice).navigation(this, REQUEST_NOTICE_DETAIL)
            }
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == REQUEST_NOTICE_DETAIL && resultCode == Activity.RESULT_OK) {
            // 消息已读，更新消息状态

            mAdapter.data.forEachIndexed { index, notice ->
                if (notice.id == detailId) {
                    notice.read = 1 //表示已读
                    mAdapter.notifyItemChanged(index)
                    return@forEachIndexed
                }
            }
        }
    }
}
